package com.jlnk.androidfmc.fragment;


import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Bundle;
import androidx.fragment.app.Fragment;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.jlnk.androidfmc.WebViewActivity;
import com.jlnk.androidfmc.adapter.NewsAdapter;
import com.jlnk.androidfmc.database.NewDatabaseHelper;
import com.jlnk.androidfmc.view.NewsPost;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import com.jlnk.androidfmc.R;

import com.jlnk.androidfmc.entity.News;
import android.app.Activity;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;


public class HotNewFragment extends Fragment {

    private ListView listView;
    private static final String COLUMN_TITLE = "title";
    private static final String COLUMN_CONTENT = "content";
    private static final String COLUMN_IMAGE = "image";
    private static final String TABLE_NEWS = "news";
    private NewsAdapter adapter; // 定义适配器变量
    private List<News> newsList; // 定义 newsList 变量
    private int IMAGE_PICK_CODE;


    public HotNewFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_hotnew, container, false);
        listView = view.findViewById(R.id.news_list_view);

        // 初始化适配器和 newsList
        newsList = new ArrayList<>();
        adapter = new NewsAdapter(getActivity(), newsList);
        listView.setAdapter(adapter);

        listView = view.findViewById(R.id.news_list_view);
        FloatingActionButton fabPublish = view.findViewById(R.id.fab_publish); // 获取FAB

        // 在HotNewFragment中为ImageView设置点击事件
        ImageView hotPhoto1 = view.findViewById(R.id.hotPhoto1);
        hotPhoto1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建Intent，用于启动新闻详情Activity
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                // 传递样式标识符，这里是默认样式
                intent.putExtra("style", "news_detail");
                // 启动新闻详情Activity
                startActivity(intent);
            }
        });

        // 设置FAB的点击事件
        fabPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到新的Activity
                Intent intent = new Intent(getActivity(), NewsPost.class);
                startActivity(intent);
            }
        });
        // 获取新闻数据并显示
        populateNewsList();

        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_PICK_CODE && resultCode == Activity.RESULT_OK) {
            if (data.hasExtra("new_news")) {
                News newNews = data.getParcelableExtra("new_news");
                addNews(newNews); // 将新新闻添加到列表中
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        refreshList();
    }
    private void populateNewsList() {
        NewDatabaseHelper dbHelper = new NewDatabaseHelper(getActivity());
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        newsList.clear();

        String[] columns = {
                COLUMN_TITLE,
                COLUMN_CONTENT,
                COLUMN_IMAGE
        };
        Cursor cursor = db.rawQuery("SELECT " + TextUtils.join(", ", columns) + " FROM " + TABLE_NEWS, null);

        if (cursor != null && cursor.moveToFirst()) {
            int titleIndex = cursor.getColumnIndex(COLUMN_TITLE);
            int contentIndex = cursor.getColumnIndex(COLUMN_CONTENT);
            int imageIndex = cursor.getColumnIndex(COLUMN_IMAGE);

            do {
                News news = new News();
                news.setTitle(cursor.getString(titleIndex));
                news.setContent(cursor.getString(contentIndex));
                byte[] imageBytes = cursor.getBlob(imageIndex);
                if (imageBytes != null && imageBytes.length > 0) {
                    news.setImageBytes(imageBytes); // 设置图片字节数据
                    Bitmap image = news.getImage(); // 这里可以获取到Bitmap对象
                    // 如果需要，可以在此处使用image对象
                }
                newsList.add(news);
            } while (cursor.moveToNext());
            cursor.close();
        }
        db.close();
        adapter.notifyDataSetChanged();
    }

    public void addNews(News news) {
        // 确保这里的 adapter 是您的新闻列表适配器
        adapter.addNews(news);
        adapter.notifyDataSetChanged(); // 通知数据已更改
    }

    public void refreshList() {
        // 重新从数据库加载新闻列表
        populateNewsList();
        // 或者如果适配器持有新闻列表，可以直接调用
        adapter.notifyDataSetChanged();
    }
}